package com.example.basic_ui_demo.view.news

import android.os.Build
import androidx.annotation.RequiresExtension
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import com.example.basic_ui_demo.companion.ApiViewModel
import com.example.basic_ui_demo.companion.news.NewsRetrofitInstance
import com.example.basic_ui_demo.view_model.NewsViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
@Composable
fun NewsSearcher(viewModel: NewsViewModel) {

    val defaultText by remember { viewModel.defaultText }
    val searchText by remember { viewModel.searchText }
    val focusRequester = remember { FocusRequester() }

    TextField(
        value = searchText,
        onValueChange = { viewModel.onTextChanged(it) },
        singleLine = true,
        label = { Text("仅支持中文搜索") },
        placeholder = { Text(text = defaultText) },
        colors = TextFieldDefaults.colors(
            unfocusedContainerColor = Color.LightGray,
            focusedContainerColor = Color.LightGray,
            unfocusedTextColor = Color.Gray,
            focusedTextColor = Color.Black,
            focusedLabelColor = Color.LightGray,
            unfocusedPlaceholderColor = Color.Gray,
            focusedPlaceholderColor = Color.LightGray
        ),
        trailingIcon = {
            IconButton(
                onClick = { searchNews(viewModel,searchText) }
            ) {
                Icon(imageVector = Icons.Rounded.Search, contentDescription = "", tint = Color.Gray)
            }
        },
        modifier = Modifier
            .fillMaxWidth()
            .focusRequester(focusRequester),
    )
}

@RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
fun searchNews(viewModel: NewsViewModel, searchText: String) {
    CoroutineScope(Dispatchers.IO).launch {
        viewModel.isLoading.value = true
        var api = NewsRetrofitInstance.api

        var newsJson = searchText.isNotBlank().let {
            ApiViewModel.callApi { api.getNews(1, bannerNum, searchText) }
        } ?: run { ApiViewModel.callApi { api.getNews(1, bannerNum) } }

        if (newsJson != null) {
            viewModel.replaceNewsList(newsJson.result?.newslist)
        } else {
            NewsRetrofitInstance.changeApi()
            api = NewsRetrofitInstance.api

            newsJson = searchText.isNotBlank().let {
                ApiViewModel.callApi { api.getNews(1, bannerNum, searchText) }
            } ?: run { ApiViewModel.callApi { api.getNews(1, bannerNum) } }

            if (newsJson != null)
                viewModel.replaceNewsList(newsJson.result?.newslist)
            else
                viewModel.isError.value = true
        }

        viewModel.isLoading.value = false
    }
}